package com.foreverwin.jzymes.qc.point.service.impl;

import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.qc.point.mapper.FindUndoneMapper;
import com.foreverwin.jzymes.qc.point.service.IFindUndoneService;
import com.foreverwin.jzymes.qc.sample.domain.SampleData;
import com.foreverwin.jzymes.qc.sample.mapper.SampleDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class FindUndoneServiceImpl implements IFindUndoneService {

    @Autowired
    FindUndoneMapper findUndoneMapper;
    @Autowired
    SampleDataMapper sampleDataMapper ;
    @Autowired
    IProductionShiftService productionShiftService;

    /**
     * 查找班次未做点检的样品信息
     *
     * @param type
     * @param site
     * @return
     */
    @Override
    public List<SampleData> findSampleUndone(String type, String site,Date now) {

        //Date now = new Date();
        Date[]  dates = findToday( site ,now );
        Date  start = dates[0];
        Date  end   = dates[1];
        List< SampleData > pointChecks = findUndoneMapper.findSampleUndone( start , end  );
        List< SampleData >  sampleDatas = findUndoneMapper.findSampleON( site );
        List< SampleData > resList = new ArrayList<>();
        if ( !pointChecks.isEmpty() && !sampleDatas.isEmpty()){
            for ( SampleData Bean : sampleDatas ){
                boolean flag = true;
                for (SampleData newBean : pointChecks) {
                    if(newBean.getHandle().equals( Bean.getHandle() ) ){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    resList.add(Bean);
                }
            }

            for ( SampleData sampleData : resList ){

                sampleData.setItemBo( sampleData.getItemBo().split( "," )[1] );
            }
            return  resList;
        }

        for ( SampleData sampleData : sampleDatas ){

            sampleData.setItemBo( sampleData.getItemBo().split( "," )[1] );
        }
        return sampleDatas;
    }

    /**
     * 查找班次未做点检的检具信息
     * @param type
     * @param site
     * @return
     */
    @Override
    public List<Map<String, Object>> findToolUndone(String type, String site , String group,Date now) {
        //Date now = new Date();
        Date[] dates = findToday( site ,now );
        Date  start = dates[0];
        Date  end   = dates[1];
        String status = "StatusBO:"+site+",302";  //禁用
        //Assert.hasText( group , I18nUtil.getI18nText("common.toolGroupNotBlank"));
        if (!StringUtils.isEmpty( group )){
            group = "ToolGroupBO:"+site+","+ group ;
        }
        List<Map<String, Object>> toolOn = findUndoneMapper.findToolOn( site , status ,group);
        /**更新逾期检具状态*/
       // updateToolStatus( toolOn , site );
        List<Map<String, Object>> toolUndone = findUndoneMapper.findToolUndone( start , end ,status );

        List< Map<String, Object> > resList = new ArrayList<>();

           if ( !toolOn.isEmpty() && !toolUndone.isEmpty()){
            for ( Map<String,Object> Bean : toolOn ){
                Map<String,Object> resMap = new HashMap<>();
                boolean flag = true;
                for (Map<String,Object> newBean : toolUndone) {
                    if(newBean.get("HANDLE").equals( Bean.get("HANDLE") ) ){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    Assert.notNull( Bean.get("EXP_DATE"),I18nUtil.getI18nText("common.toolNumberDateNotExist",new String []{ (String) Bean.get("TOOL_NUMBER") }) );
                    /**补充字段*/
                    updateToolStatus( Bean , site);
                    resMap.put("handle",Bean.get("HANDLE"));
                    resMap.put("number",Bean.get("TOOL_NUMBER"));
                    resMap.put("description",Bean.get("DESCRIPTION"));
                    resMap.put("qty",Bean.get("QTY"));
                    resMap.put("status",Bean.get("status"));
                    /*Calendar cal = Calendar.getInstance();
                    cal.setTime( (Date) Bean.get("EXP_DATE") );
                    cal.add(Calendar.HOUR_OF_DAY, +8);*/
                    resMap.put("expDate", (Date) Bean.get("EXP_DATE"));
                    resMap.put("group",group);

                    resList.add(resMap);

                }
             }

             return  resList;
           }


        for (  Map<String,Object> map : toolOn ){
            Map<String,Object> resMap = new HashMap<>();
            Assert.notNull( map.get("EXP_DATE"),I18nUtil.getI18nText("common.toolNumberDateNotExist",new String []{ (String) map.get("TOOL_NUMBER") }) );
            /**补充字段*/
            updateToolStatus( map , site);
            resMap.put("handle",map.get("HANDLE"));
            resMap.put("number",map.get("TOOL_NUMBER"));
            resMap.put("description",map.get("DESCRIPTION"));
            resMap.put("status",map.get("status"));
            resMap.put("qty",map.get("QTY"));
            /*Calendar cal = Calendar.getInstance();
            cal.setTime( (Date) map.get("EXP_DATE") );
            cal.add(Calendar.HOUR_OF_DAY, +8);*/
            resMap.put("expDate", (Date) map.get("EXP_DATE"));
            resMap.put("group",group);
            resList.add( resMap );

        }

        return resList;
    }

    /**
     * 获取班次的开始/结束时间
     * @return
     */
    public Date[] findToday( String site , Date now ){

        String shift =  productionShiftService.findShift( site ,now );
        Date[] dates = productionShiftService.findShiftRange( site , shift , now );
        /*Calendar start = new GregorianCalendar();
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);
        dates.add( start.getTime() );

        Calendar end = new GregorianCalendar();
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 999);
        dates.add( end.getTime() );*/

        return  dates;
    }

    /**
     * 更新逾期工具状态
     * @param map
     * @param site
     */
    public void updateToolStatus( Map<String, Object> map,String site ){
           //String status = "StatusBO:"+site+",2";
          // Assert.notNull( map.get("EXP_DATE"),I18nUtil.getI18nText("common.toolNumberDateNotExist",new String []{ (String) map.get("TOOL_NUMBER") }) );
           Date expDate =  (Date) map.get("EXP_DATE");
           Calendar cal = Calendar.getInstance();
           cal.setTime(expDate);
           cal.add(Calendar.HOUR_OF_DAY, +8);
           Date now = new Date();
           boolean flag = now.before( cal.getTime() );
           map.put("EXP_DATE",cal.getTime() );
           if ( !flag ){
            map.put("status","逾期");
            //findUndoneMapper.updateToolNumberStatus( status ,(String) map.get("Handle")  );
           }else {
               Calendar ca = Calendar.getInstance();
               ca.setTime( now );
               ca.add(Calendar.DATE, +1);
               if ( (cal.getTime()).before( ca.getTime() ) ){
                   map.put("status","提醒");
               }else {
                   map.put("status","已启用");
               }
           }

    }

    /**
     * 检具状态更改
     * @param resMap
     * @param site
     */
    public void exchageStatusDescription(  Map<String, Object>  resMap ,String site){

          String status = (String)resMap.get("STATUS_BO");
          if( status.equals( "StatusBO:" + site + ",301" ) ){
              resMap.put("STATUS_BO","已启用");
          }
          if( status.equals( "StatusBO:" + site + ",302" ) ){
              resMap.put("STATUS_BO","已禁用");
          }
    }
}
